const moviesModel = require('../model/moviesModel')

class PositionController {

  //职位添加
  async add(req, res) {
    try {
      req.body.createTime = new Date().toLocaleDateString()
      let rs = await moviesModel.save(req.body);
      console.log(rs);
      if (rs) {
        //发送实时通知
        socketio.emit('message', {
          code: 1,
          message: "添加电影"
        })
        res.send({
          code: 1,
          message: '添加电影成功'
        })
      }
    } catch (error) {
      console.log(error)
      res.send({
        code: -1,
        message: '添加电影信息失败'
      })
    }
  }

  //职位查询
  async list(req, res) {
    let { nm, cityId, pagesize, pageno, sortname, sort } = req.query;
    let searchParams = {};
    if (nm) {
      searchParams['nm'] = new RegExp(nm,'i');
    }
    if (cityId) {
      searchParams['cityId'] = cityId;
    }
    let data = await moviesModel.query(searchParams, pagesize, pageno, sortname, sort)
    let total = await moviesModel.count(searchParams)
    res.send({
      code: 1,
      data,
      total
    })

  }

  //查询一个职位
  async findone(req, res) {
    let id = req.params.id;
    let data = await moviesModel.findone(id)
    res.send({
      code: 1,
      data
    })
  }

  //更新职位
  async update(req, res) {
    let { _id } = req.body;
    delete req.body._id;
    let rs = await moviesModel.update(_id, req.body)
    if (rs.nModified) {
      socketio.emit('message', {
        code: 2,
        message: "修改职位信息成功"
      })
      res.send({
        code: 1,
        message: '更新职位信息成功'
      })
    } else {
      res.send({
        code: -1,
        message: '更新职位信息失败'
      })
    }
  }

  //删除职位
  async del(req, res) {
    console.log('del:' + req.body.ids);
    let { ids } = req.body;
    let rs = await moviesModel.remove(ids.split(','));
    console.log(rs);
    if (rs.deletedCount) {
      res.send({
        code: 1,
        message: "删除数据成功"
      })
    } else {
      res.send({
        code: -1,
        message: "删除数据失败"
      })
    }

  }


}

module.exports = new PositionController();